﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

/// <summary>
/// System 工具
/// </summary>
public static class SystemUtil
{
    public static int ToInt(this string str)
    {
        return int.Parse(str);
    }

    public static long ToLong(this string str)
    {
        return long.Parse(str);
    }

    public static bool ToBool(this string str)
    {
        return str.Equals("True", StringComparison.CurrentCultureIgnoreCase);
    }

    public static bool IsNull<T>(this T[] array)
    {
        return array == null || array.Length <= 0;
    }

    public static bool IsNull<T>(this List<T> array)
    {
        return array == null || array.Count <= 0;
    }

    public static string ToStringExt<T>(this T[] array, string sign = null)
    {
        StringBuilder bulider = new StringBuilder();

        for (int i = 0; i < array.Length; i++)
        {
            bulider.Append(array[i]);
            bulider.Append(sign);
        }
        return bulider.ToString();
    }


    /// <summary>
    /// 查询
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="Q"></typeparam>
    /// <param name="array"></param>
    /// <param name="condition"></param>
    /// <returns></returns>
    public static Q[] Select<T, Q>(this T[] array, Func<T, Q> condition)
    {
        if (IsNull(array)) return null;

        //存储筛选出来满足条件的元素
        List<Q> result = new List<Q>();
        for (int i = 0; i < array.Length; i++)
        {
            //筛选的条件【满足筛选条件，将该元素存储到result】
            if (condition(array[i]) != null)
                result.Add(condition(array[i]));
        }
        return result.ToArray();
    }

    /// <summary>
    /// 最小对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="Q"></typeparam>
    /// <param name="array"></param>
    /// <param name="condition"></param>
    /// <returns></returns>
    public static T GetMin<T, Q>(this T[] array, Func<T, Q> condition) where Q : IComparable
    {
        if (IsNull(array)) return default(T);

        T min = array[0];
        for (int i = 1; i < array.Length; i++)
        {
            if (condition(min).CompareTo(condition(array[i])) > 0)
            {
                min = array[i];
            }
        }
        return min;
    }

    /// <summary>
    /// 最小对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="Q"></typeparam>
    /// <param name="array"></param>
    /// <param name="condition"></param>
    /// <returns></returns>
    public static T[] GetMinArr<T, Q>(this T[] array, Func<T, Q> condition) where Q : IComparable
    {
        if (IsNull(array)) return null;

        List<T> UserList = new List<T>();
        T min = array[0];
        UserList.Add(min);

        for (int i = 1; i < array.Length; i++)
        {
            if (condition(min).CompareTo(condition(array[i])) > 0)
            {
                min = array[i];
                UserList.Clear();
            }

            if (condition(min).CompareTo(condition(array[i])) == 0)
            {
                UserList.Add(array[i]);
            }
        }
        return UserList.ToArray();
    }

    /// <summary>
    /// 排序  Sort(Array[],(a,b)=>a.x>b.x)  >降序  <升序 ;
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    /// <param name="array">数组</param>
    /// <param name="handler">委托</param>
    public static void SortArr<T>(this T[] array, Func<T, T, bool> handler)
    {
        if (IsNull(array)) return;
        if (array.Length <= 1) return;

        for (int i = 0; i < array.Length - 1; i++)
        {
            for (int j = i + 1; j < array.Length; j++)
            {
                if (!handler(array[i], array[j]))
                {
                    T temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    /// <summary>
    /// 查询 孤舰
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="array"></param>
    /// <param name="condition"></param>
    /// <returns></returns>
    public static T[] SelectArr<T>(this T[] array, Func<T, T, bool> handler)
    {
        if (IsNull(array)) return null;
        if (array.Length <= 1) return array;

        int minUserCount = int.MaxValue;
        List<T> UserList = new List<T>();
        for (int i = 0; i < array.Length; i++)
        {
            int UserCount = 0;
            for (int j = 0; j < array.Length; j++)
            {
                if (i != j && handler(array[i], array[j]))
                {
                    UserCount++;
                }
            }
            if (minUserCount > UserCount)
            {
                minUserCount = UserCount;
                UserList.Clear();
            }
            if (minUserCount == UserCount)
                UserList.Add(array[i]);
        }
        return UserList.ToArray();
    }


    public static string GetTypeStr(this System.Type type)
    {

        if (type == typeof(string))
        {
            return "string";
        }
        else if (type == typeof(float))
        {
            return "float";
        }
        else if (type == typeof(int))
        {
            return "int";
        }
        else if (type == typeof(long))
        {
            return "long";
        }
        else if (type == typeof(double))
        {
            return "double";
        }
        else if (type == typeof(byte))
        {
            return "byte";
        }
        else if (type == typeof(DateTime))
        {
            return "DateTime";
        }

        return type.Name;
    }

    public static object GetTypeStr(this string Value, string FieldType)
    {
        switch (FieldType)
        {
            case "string": return Value;
            case "float": return float.Parse(Value);
            case "int": return int.Parse(Value);
            case "long": return long.Parse(Value);
            case "double": return double.Parse(Value);
            case "byte": return byte.Parse(Value);
            case "DateTime": return System.DateTime.Parse(Value);

            default: break;
        }

        return Value;
    }

    public static object GetTypeStr(this string Value, FieldInfo info)
    {
        
        switch (info.FieldType.ToString())
        { 
            case "System.String": return Value;
            case "System.Single": return float.Parse(Value);
            case "System.Int32": return int.Parse(Value);
            case "System.Int64": return long.Parse(Value);
            //case "double": return double.Parse(Value);
            //case "byte": return byte.Parse(Value);
            case "System.DateTime": return System.DateTime.Parse(Value);

            default: break;
        }

        return Value;
    }
}
